﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW.BLL
{
    public class RoleService : RepositoryBase
    {
        public IQueryable<UserType> GetRoleList()
        {
            return GetEntities<UserType>();
        }

        public List<RolePermissionModel> GetRolePermissionByGroupID(int roleID, int groupID)
        {
            List<RolePermissionModel> models = new List<RolePermissionModel>();
            IQueryable<Menu> menus = GetEntities<Menu>().Where(m => m.ParentID == groupID);
            foreach (Menu menu in menus)
            {
                RolePermissionModel model = new RolePermissionModel();
                model.MenuID = menu.ID.ToString();
                model.MenuName = menu.Name;

                RoleMenu roleMenu = GetEntities<RoleMenu>().Where(r => r.UserType.ID == roleID && r.Menu.ID == menu.ID).FirstOrDefault();
                if (roleMenu != null)
                {
                    switch (roleMenu.Permission)
                    {
                        case 0:
                        model.View = false;
                        model.Operate = false;
                        break;
                        case 1:
                        model.View = true;
                        model.Operate = false;
                        break;
                        case 2:
                        model.View = true;
                        model.Operate = true;
                        break;
                    }
                }
                models.Add(model);
            }
            return models;
        }

        public void UpdateRolePermission(int roleID, int menuID, int permission)
        {
            RoleMenu roleMenu = GetEntities<RoleMenu>().Where(r => r.UserType.ID == roleID && r.Menu.ID == menuID).FirstOrDefault();
            if (roleMenu != null)
            {
                roleMenu.Permission = permission;
                UpdateEntity(roleMenu);
            }
            else
            {
                roleMenu = new RoleMenu();
                roleMenu.Menu = GetEntities<Menu>().Where(m => m.ID == menuID).FirstOrDefault();
                roleMenu.UserType = GetEntities<UserType>().Where(u => u.ID == roleID).FirstOrDefault();
                roleMenu.Permission = permission;
                InsertEntity(roleMenu);
            }
        }

        public void UpdateRole(UserType userType)
        {
            UpdateEntity(userType);
        }

        public void AddRole(UserType userType)
        {
            UserType existUserType = GetEntities<UserType>().Where(u => u.Name.Equals(userType.Name)).FirstOrDefault();
            if (existUserType == null)
            {
                InsertEntity(userType);
            }
            else
            {
                existUserType.Description = userType.Description;
                UpdateEntity(existUserType);
            }
        }

        public int GetCountOfUserType()
        {
            return GetEntities<UserType>().Count();
        }

        public List<User> GetUserHasPermission(int menuID)
        {
            List<User> userList = new List<User>();
            IQueryable<RoleMenu> rolePermission = GetEntities<RoleMenu>().Where(r => r.Menu.ID == menuID && r.Permission == 2);
            foreach (RoleMenu roleMenu in rolePermission)
            {
                if (!roleMenu.UserTypeReference.IsLoaded)
                {
                    roleMenu.UserTypeReference.Load();
                }
                if (!roleMenu.UserType.User.IsLoaded)
                {
                    roleMenu.UserType.User.Load();
                }
                foreach (User user in roleMenu.UserType.User)
                {
                    userList.Add(user);
                }
            }
            return userList;
        }
    } 
}
